package com.gombe.credit.app.common

import com.gombe.credit.app.GombeCreditApp
import com.gombe.credit.app.base.BasePresenter
import com.gombe.credit.app.request.IHttpCallback
import org.json.JSONObject

/**
 * CommonPresenter
 * @Author wwdeng
 * @CreateTime: 2024/11/20 10:32
 * @Annotation：
 */
abstract class CommonPresenter(private val iView: ICommonView?): BasePresenter() {

    /**
     * 接口请求
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        iView?.let {
            it.getAct().doPostAction(httpUrl, params,object : IHttpCallback{
                override fun onRequestSuccess(data: JSONObject) {
                    when(data.optInt("code")) {
                        0 -> {//成功
                            callback.onRequestSuccess(data)
                        }

                        else -> {
                            callback.onRequestError(data)
                            val msg = data.optString("msg")
                            it.showGombeCreditToast(msg)
                            it.hideGombeCreditLoading()
                        }
                    }
                }

                override fun onRequestError(error: JSONObject) {
                    callback.onRequestError(error)
                    it.hideGombeCreditLoading()
                    if (!it.getAct().isFinishing && !it.getAct().isDestroyed){
                        it.getAct().showRequestErrorDialog(error)
                    }
                }

                override fun onRequestFinish() {
                    callback.onRequestFinish()
                }
            })
        }
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        iView?.getAct()?.addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * getString
     */
    fun getString(strId: Int): String{
        return GombeCreditApp.app.getString(strId)
    }
}